//! 简单的INSERT语句测试
//!
//! 测试INSERT语句的基本功能

use cdb::sql::QueryEngine;
use cdb::storage::StorageEngine;
use cdb::config::DatabaseConfig;
use std::sync::Arc;
use parking_lot::RwLock;

/// 创建测试用的查询引擎
fn create_test_query_engine() -> QueryEngine {
    let config = DatabaseConfig::default();
    let storage_engine = Arc::new(RwLock::new(StorageEngine::new(&config).unwrap()));
    QueryEngine::new(storage_engine)
}

#[test]
fn test_simple_insert() {
    let mut engine = create_test_query_engine();

    // 使用唯一的表名避免冲突
    let table_name = format!("test_table_{}", std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos());
    let create_sql = format!("CREATE TABLE {} (id INTEGER PRIMARY KEY, name TEXT NOT NULL)", table_name);
    let result = engine.execute(&create_sql);
    
    match result {
        Ok(res) => {
            assert_eq!(res.get_affected_rows(), 1);
            println!("✅ 表创建成功");
        }
        Err(e) => {
            println!("❌ 表创建失败: {:?}", e);
            panic!("表创建失败");
        }
    }

    // 测试基础INSERT语句
    let insert_sql = format!("INSERT INTO {} VALUES (1, 'Alice')", table_name);
    let result = engine.execute(&insert_sql);

    match result {
        Ok(res) => {
            assert_eq!(res.get_affected_rows(), 1);
            println!("✅ INSERT语句执行成功，插入了 {} 行", res.get_affected_rows());
        }
        Err(e) => {
            println!("❌ INSERT语句执行失败: {:?}", e);
            panic!("INSERT语句执行失败");
        }
    }

    // 测试多行INSERT语句
    let insert_sql = format!("INSERT INTO {} VALUES (2, 'Bob'), (3, 'Charlie')", table_name);
    let result = engine.execute(&insert_sql);

    match result {
        Ok(res) => {
            assert_eq!(res.get_affected_rows(), 2);
            println!("✅ 多行INSERT语句执行成功，插入了 {} 行", res.get_affected_rows());
        }
        Err(e) => {
            println!("❌ 多行INSERT语句执行失败: {:?}", e);
            // 这个可能还没实现，所以不panic
        }
    }
}
